package com.flydog.taskManager.controller;

import com.flydog.taskManager.common.api.CommonResult;
import com.flydog.taskManager.dto.PeriodicTaskParam;
import com.flydog.taskManager.entity.PeriodicTask;
import com.flydog.taskManager.entity.PhasedTask;
import com.flydog.taskManager.service.PeriodicTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 周期性工作控制类
 *
 * @author caoweidong
 * @version 0.0.0
 * @create 2021/6/18 10:45
 */

@RestController
@RequestMapping("/periodic")
@Slf4j
public class PeriodicTaskController {
    @Autowired
    PeriodicTaskService periodicTaskService;

    @PostMapping(value = "/create")
    public CommonResult<PeriodicTask> create(@RequestBody PeriodicTaskParam param) {
        return CommonResult.success(periodicTaskService.create(param));
    }

    @GetMapping("/{id}")
    CommonResult<PeriodicTask> getById(@PathVariable("id") Long id) {
        return periodicTaskService.getItem(id).map(CommonResult::success).orElseGet(CommonResult::failed);
    }

    @GetMapping(value = "/listAll")
    public CommonResult<List<PeriodicTask>> listAll() {
        return CommonResult.success(periodicTaskService.listAll());
    }

    @PostMapping(value = "/update/{id}")
    public CommonResult<PeriodicTask> update(@PathVariable("id") Long id, @RequestBody PeriodicTaskParam param) {
        return CommonResult.success(periodicTaskService.update(id, param));
    }

    @PostMapping(value = "/delete")
    public CommonResult<Set<Long>> delete(@RequestParam Long[] ids) {
        return CommonResult.success(periodicTaskService.delete(ids));
    }

    @PostMapping(value = "/updateProcessStatus")
    CommonResult<PeriodicTask> updateProcessStatus(@RequestParam Long id, @RequestParam int status) {
        return CommonResult.success(periodicTaskService.updateCurrentStatus(id, status));
    }
}
